<?php 
//载入配置文件
require_once '../config.php';
//给用户一个箱子，如果之前有就用之前的
session_start();

function login(){
  //1.接收并校验
  if (empty($_POST['email'])) {
    $GLOBALS['message'] = '请输入邮箱';
    return;
  }
   if (empty($_POST['password'])) {
    $GLOBALS['message'] = '请输入密码';
    return;
  }
  //2.持久化
  $email = $_POST['email'];
  $password = $_POST['password'];
  //当用户提交完整表信息就应该对其进行校验
  $conn = mysqli_connect(XIU_DB_HOST, XIU_DB_USER, XIU_DB_PASS, XIU_DB_NAME);
  if (!$conn) {
    exit('<h1>连接数据库失败</h1>');
  }
    $query = mysqli_query($conn,"select * from users where email = '{$email}' limit 1 ;");
    if (!$query) {
      $GLOBALS['message'] = '登录失败,请重试';
      return;
    }
    //获取登录用户
    $users = mysqli_fetch_assoc($query);
    if (!$users) {
      //用户名不存在
      $GLOBALS['message'] = '密码与邮箱不匹配';
      return;
    }
    if ($users['password'] !== $password) {
      //密码错误
      $GLOBALS['message'] = '密码与邮箱不匹配';
      return;
    }
    //存一个登录标识
    $_SESSION['login_user'] = $users;
  
  //3.响应
  header('Location:/admin/index.php');
}  


if ($_SERVER['REQUEST_METHOD'] =='POST') {
  login();
}
//退出登录
if ($_SERVER['REQUEST_METHOD'] =='GET' && isset($_GET['action']) && $_GET['action'] == 'logout') {
  unset($_SESSION['login_user']);
}
 ?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="utf-8">
  <title>Sign in &laquo; Admin</title>
  <link rel="stylesheet" href="/static/assets/vendors/bootstrap/css/bootstrap.css">
  <link rel="stylesheet" href="/static/assets/css/admin.css">
  <link rel="stylesheet" href="/static/assets/vendors/animate/animate.css">
</head>
<body>
  <div class="login">
    <!-- 可以通过在form上添加 novalidate 取消浏览器自带的校验功能-->
    <!-- autocomplete="off" 关闭客户端的自动完成功能-->
    <form class="login-wrap<?php echo isset($message) ? ' shake animated': ''; ?>" action="<?php echo $_SERVER['PHP_SELF'] ?>" method='post' autocomplete="off" novalidate>
      <img class="avatar" src="/static/assets/img/default.png">
      <!-- 有错误信息时展示 -->
      <?php if (isset($message)): ?>
        <div class="alert alert-danger">
        <strong>错误！</strong> <?php echo $message ?>
      </div>
      <?php endif ?>
      <div class="form-group">
        <label for="email" class="sr-only">邮箱</label>
        <input id="email" name="email" type="email" class="form-control" placeholder="邮箱" autofocus value="<?php echo empty($_POST['email'])? '':$_POST['email'] ?>">
      </div>
      <div class="form-group">
        <label for="password" class="sr-only">密码</label>
        <input id="password" name="password" type="password" class="form-control" placeholder="密码">
      </div>
      <button class="btn btn-primary btn-block" href="index.html">登 录</button>
    </form>
  </div>
  <script src="/static/assets/vendors/jquery/jquery.js"></script>
  <script>
   
    $(function($){
    //为了让代码在页面加载完成后执行
    //单独作用域
    
    //目标：当用户输入自己邮箱后，获取邮箱对应的头像
    //实现：
    //-时机：邮箱文本框失去焦点
    //-事情：获取邮箱文本框所对应的头像的地址，展示到image元素上
    $('#email').on('blur',  function() {
      //判断邮箱是否为空且是否合法
      var checkemail = /^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
      var value = $(this).val();
      if(!value || !checkemail.test(value)) return;
      //console.log(value);
      //得到正确邮箱
      $.get('/admin/api/avatar.php',{email:value},function(res) {
       //=>希望res得到这个邮箱对应的头像地址
       if(!res) return;
       $('.avatar').attr('src',res);
      });

    });
    });
  </script>
</body>
</html>
